/**
 * Copyright (c) 2016-2023, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress.module.article.controller.admin;

import com.jfinal.aop.Inject;
import io.jboot.db.model.Columns;
import io.jboot.utils.DateUtil;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jpress.JPressConsts;
import io.jpress.core.menu.annotation.AdminMenu;
import io.jpress.module.article.ArticlePermissionGroups;
import io.jpress.module.article.model.Article;
import io.jpress.module.article.service.ArticleService;
import io.jpress.permission.annotation.PermissionDef;
import io.jpress.web.base.AdminControllerBase;

import java.util.List;

/**
 * @author Michael Yang 杨福海 （fuhai999@gmail.com）
 * @version V1.0
 */
@RequestMapping(value = "/admin/article/views", viewPath = JPressConsts.DEFAULT_ADMIN_VIEW)
public class _ArticleViewsController extends AdminControllerBase {

    @Inject
    private ArticleService articleService;


    @PermissionDef(title = "查看文章访问排行", groupId = ArticlePermissionGroups.ARTICLE)
    @AdminMenu(text = "文章访问排行", groupId = JPressConsts.SYSTEM_MENU_STATISTICS, order = 0)
    public void index() {

        Columns baseColumns = Columns.create().eq("article.status", Article.STATUS_NORMAL);

        Columns thisWeekCond = new Columns();
        thisWeekCond.append(baseColumns);
        thisWeekCond.ge("article.created", DateUtil.getStartOfThisWeek());
        List<Article> thisWeekArticles = articleService.findListByColumns(thisWeekCond, "view_count desc", 10);

        Columns thisMonthCond = new Columns();
        thisMonthCond.append(baseColumns);
        thisMonthCond.ge("article.created", DateUtil.getStartOfThisMonth());
        List<Article> thisMonthArticles = articleService.findListByColumns(thisMonthCond, "view_count desc", 10);


        Columns thisQuarterCond = new Columns();
        thisQuarterCond.append(baseColumns);
        thisQuarterCond.ge("article.created", DateUtil.getStartOfThisQuarter());
        List<Article> thisQuarterCondArticles = articleService.findListByColumns(thisQuarterCond, "view_count desc", 10);


        Columns thisYearCond = new Columns();
        thisYearCond.append(baseColumns);
        thisYearCond.ge("article.created", DateUtil.getStartOfThisYear());
        List<Article> thisYearCondArticles = articleService.findListByColumns(thisYearCond, "view_count desc", 10);


        setAttr("thisWeekArticles", thisWeekArticles);
        setAttr("thisMonthArticles", thisMonthArticles);
        setAttr("thisQuarterArticles", thisQuarterCondArticles);
        setAttr("thisYearArticles", thisYearCondArticles);


        render("article/article_statistics_views.html");
    }


}
